﻿using System;
using NUnit.Framework;
using Smartkernel;
using Smartkernel.Interfaces;

namespace Smartkernel.NUnitTest.Interfaces
{
    [Category("Interfaces")]
    [TestFixture]
    public class ISmartConvertFromStringTest
    {
        [Test]
        public void Test()
        {
            //【01】从字符串形式转换为指定形式（涉及方法：ConvertTo）
            MyType myType = SmartReflection.ConvertTo<MyType>("张三|27");
            Assert.AreEqual(myType.Name, "张三");
            Assert.AreEqual(myType.Age, 27);
        }

        private class MyType : ISmartConvertFromString
        {
            private string name;
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
            private int age;
            public int Age
            {
                get
                {
                    return this.age;
                }
                set
                {
                    this.age = value;
                }
            }

            public object ConvertFrom(string source)
            {
                string[] datas = source.Split(new char[] { '|' });
                MyType myType = new MyType();
                myType.name = datas[0];
                myType.age = Convert.ToInt32(datas[1]);
                return myType;
            }
        }
    }
}
